package utils;

import com.example.crawler.Values;
import com.example.crawler.constant.StaticConstant;
import com.google.common.collect.Lists;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author pq
 * @date 2022/10/27 22:35
 * @description 解析页面工具类
 * @modified
 **/
public class ParsePageUtils {
    /***
     * @author pq
     * @description 解析网页
     * @param url
     * @return java.lang.String
     * @date 2022/10/27 22:47
     */
    public static String parsePageToString(String url) {
        HttpClient client = new HttpClient();
        // 设置代理服务器地址和端口
        //client.getHostConfiguration().setProxy("proxy_host_addr",proxy_port);
        // 使用 GET 方法 ，如果服务器需要通过 HTTPS 连接，那只需要将下面 URL 中的 http 换成 https
        HttpMethod method = new GetMethod(url);
        //使用POST方法
        //HttpMethod method = new PostMethod("http://java.sun.com");
        try {
            client.executeMethod(method);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String webpageStr = "";
        try {
            webpageStr = method.getResponseBodyAsString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //释放连接
        method.releaseConnection();
        return webpageStr;
    }

    /***
     * @author pq
     * @description 从网页字符串中获取网页链接
     * @param webPageStr
     * @return java.util.List<java.lang.String>
     * @date 2022/10/27 22:50
     */
    public static List<String> getPageLink(String webPageStr) {
        List<String> webPageUrlList = Lists.newArrayList();
        String pattern = "(ht|f)tp(s?)\\:\\/\\/[0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*(:(0-9)*)*(\\/?)([a-zA-Z0-9\\-\\.\\?\\,\\'\\/\\\\&%\\+\\$#_=]*)?";
        Pattern p = Pattern.compile(pattern, 2 | Pattern.DOTALL);
        Matcher m = p.matcher(webPageStr);
        while (m.find()) {
            webPageUrlList.add(m.group());
        }
        return webPageUrlList.stream().distinct().collect(Collectors.toList());
    }

    /***
     * @author pq
     * @description 从网页字符串获取图片标签
     * @param pageStr
     * @return java.util.List<java.lang.String>
     * @date 2022/10/28 0:28
     */
    public static List<String> getImgUrls(String pageStr) {
        if(StringUtils.isEmpty(pageStr)){
            return null;
        }
        List<String> imgLabels = Lists.newArrayList(); // 因文件可能有多张图片，故用集合来存储结果
        Pattern labelCompile;
        labelCompile = Pattern.compile("<img.*?>"); // "<img.*?>" : 获取标签的正则
        Matcher labelMatcher = labelCompile.matcher(pageStr);
        List<String> imgUrl = Lists.newArrayList();
        //获取图片标签
        while (labelMatcher.find()) {
            imgLabels.add(labelMatcher.group());
        }

//        Pattern imgPattern2 = Pattern.compile("<img src=\"(\\.\\.\\/).*(.png|jpg|webp)");
        Pattern imgPattern2 = Pattern.compile("(\\.\\.\\/).*(.png|jpg|webp)");
        Pattern imgPattern3 = Pattern.compile("<img src=\"(/)([^:<>\"]*)(\\.((png!thumbnail)|(png)|(jpg)|(webp)))");
        for (String imgLabel : imgLabels) {
            Matcher imgMatcher = imgPattern2.matcher(imgLabel);
            //匹配../开头的路径
            if (imgMatcher.find()) {
                String[] arr = imgMatcher.group().split("\\.\\./");
                imgUrl.add(Values.ROOT_URL + "/" + arr[arr.length - 1]);
            }
            Matcher imgMatcher1 = imgPattern3.matcher(imgLabel);
            //匹配/开头的路径
            if (imgMatcher1.find()) {
                imgUrl.add(Values.ROOT_URL + imgMatcher1.group().split("img src=\"")[1]);
            }
            Pattern imgPattern1 = Pattern.compile("(ht|f)tp(s?)\\:\\/\\/([^:<>\"]*)(\\.((png!thumbnail)|(png)|(jpg)|(webp)))");
            Matcher imgMatcher2 = imgPattern1.matcher(imgLabel);
            //匹配http或https开头的路径
            if (imgMatcher2.find()) {
                imgUrl.add(imgMatcher2.group());
            }
        }
        return imgUrl.stream().distinct().collect(Collectors.toList());
    }

    /***
     * @author pq
     * @description 保存图片到指定路径
     * @param imageUrl
     * @param formatName
     * @param storePath
     * @return void
     * @date 2022/10/28 14:14
     */
    public static void downloadImage(String imageUrl, String formatName, String storePath) {
        InputStream is = null;
        FileOutputStream fos = null;
        File storeDir = new File(storePath);
        if(!storeDir.exists()){
            storeDir.mkdirs();
        }
        String[] str =imageUrl.substring(imageUrl.lastIndexOf("/")+1).split("\\.");
        String suffix =null;
        if(str.length==1){
            suffix="jpg";
        }else {
            suffix = str[1];
        }
        String filename = formatName+"."+suffix;

        try {
            URL url = new URL(imageUrl);
            URLConnection con = url.openConnection();
            is = con.getInputStream();
            fos = new FileOutputStream(new File(storeDir,filename));
            byte[] buff = new byte[1024];
            int len = 0;
            while ((len = is.read(buff)) != -1) {
                fos.write(buff, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("保存图片失败");
        }finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
